<template>
  <view class="pageClass">
    <up-popup :show="showTemReject" mode="bottom">
      <view class="btn-group">
        <text class="cancel" @click="emits('closeFn')">取消</text>
        <text class="confirm" @click="handleConfirm">确定</text>
      </view>
      <view class="popup-content">
        <view>
          <view class="upflex10 algn">
            <view> 请选择模板：</view>
            <view>
              <radio-group @change="onGenderChange">
                <label
                  class="mgr40"
                  v-for="item in memberStore.tempLists"
                  :key="item.value"
                >
                  <radio
                    :value="item.value"
                    :checked="currentForm.abnormal == item.value"
                  />
                  {{ item.label }}
                </label>
              </radio-group>
            </view>
          </view>
          <div v-show="currentForm.abnormal == 0">
            <view class="fenlun" style="margin: 60rpx 0">
              <view class="spacenowrap">品名：</view>
              <up-picker-data
                v-model="currentForm.productName"
                title="请选择品名"
                :options="memberStore.pinnameLists"
                valueKey="value"
                labelKey="label"
              >
              </up-picker-data>
            </view>
            <view class="fenlun" style="margin: 60rpx 0">
              <view class="spacenowrap">标准：</view>
              <up-picker-data
                v-model="currentForm.standard"
                title="请选择标准"
                :options="memberStore.biaozhunLists"
                valueKey="value"
                labelKey="label"
              >
              </up-picker-data>
            </view>
          </div>
        </view>
      </view>
    </up-popup>
    <printComponent
      :imgSrc="imgSrc"
      :showReject="showReject"
      @closeFn="showReject = false"
      ref="printrefComponent"
      titleName="查看标签二维码"
    />
  </view>
</template>

<script setup>
import { useMemberStore } from "@/stores/modules/member";
import { templateFn } from "./index.js";
const deprops = defineProps(["showTemReject", "currentObject"]);
const emits = defineEmits(["closeFn"]);
const memberStore = useMemberStore();
const showReject = ref(false);
const imgSrc = ref("");
const currentForm = ref({});
const printrefComponent = ref(null);
const onGenderChange = (e) => {
  currentForm.value.abnormal = e.detail.value;
};
const handleConfirm = async () => {
  try {
    currentForm.value.id = deprops.currentObject.id;
    let obj = {};
    if (currentForm.value.abnormal == 0) {
      if (!currentForm.value.productName) {
        return uni.showToast({
          icon: "none",
          title: "请选择品名",
        });
      }
      if (!currentForm.value.standard) {
        return uni.showToast({
          icon: "none",
          title: "请选择标准",
        });
      }
      obj = currentForm.value;
    } else {
      obj = {
        id: currentForm.value.id,
        abnormal: currentForm.value.abnormal,
      };
    }
    console.log("obj", obj);
    uni.showLoading({
      title: "加载中",
    });
    await templateFn(obj);
    emits("closeFn");
    showReject.value = true;
    uni.showToast({ icon: "success", title: "保存模板成功" });
  } catch (err) {
    emits("closeFn");
  } finally {
    uni.hideLoading();
  }
};

onMounted(async () => {
  await Promise.all([
    memberStore.apiDicttemlocation("finished_template"),
    memberStore.apiDictbiaonlocation("quality_standard"),
    memberStore.apiDictpinnlocation("product_description"),
  ]);
  const obj1 = memberStore.tempLists[0] || {};
  const obj2 = memberStore.pinnameLists[0] || {};
  const obj3 = memberStore.biaozhunLists[0] || {};
  currentForm.value = {
    abnormal: obj1.value || "",
    productName: obj2.value || "",
    standard: obj3.value || "",
  };
});
</script>

<style lang="scss" scoped>
</style>